"""
可视化主题模块 - 提供图表主题设置
"""

import plotly.graph_objects as go
import plotly.io as pio
from typing import Dict, Any, Optional

# 定义主题颜色
THEME_COLORS = {
    "default": {
        "primary": "#1f77b4",
        "secondary": "#ff7f0e",
        "tertiary": "#2ca02c",
        "quaternary": "#d62728",
        "quinary": "#9467bd",
        "background": "#ffffff",
        "text": "#333333",
        "grid": "#eeeeee",
        "up": "red",
        "down": "green"
    },
    "dark": {
        "primary": "#4e79a7",
        "secondary": "#f28e2c",
        "tertiary": "#59a14f",
        "quaternary": "#e15759",
        "quinary": "#b07aa1",
        "background": "#222222",
        "text": "#ffffff",
        "grid": "#444444",
        "up": "#ff4d4d",
        "down": "#4dff4d"
    },
    "light": {
        "primary": "#4e79a7",
        "secondary": "#f28e2c",
        "tertiary": "#59a14f",
        "quaternary": "#e15759",
        "quinary": "#b07aa1",
        "background": "#f8f9fa",
        "text": "#333333",
        "grid": "#dddddd",
        "up": "#ff4d4d",
        "down": "#4dff4d"
    },
    "colorblind": {
        "primary": "#0072b2",
        "secondary": "#e69f00",
        "tertiary": "#009e73",
        "quaternary": "#cc79a7",
        "quinary": "#56b4e9",
        "background": "#ffffff",
        "text": "#333333",
        "grid": "#eeeeee",
        "up": "#e69f00",
        "down": "#0072b2"
    }
}

def apply_theme(fig: go.Figure, theme: str = "default") -> go.Figure:
    """
    应用主题到图表
    
    参数:
        fig: plotly图表对象
        theme: 主题名称，可选值: "default", "dark", "light", "colorblind"
    
    返回:
        应用了主题的图表对象
    """
    # 获取主题颜色
    colors = THEME_COLORS.get(theme, THEME_COLORS["default"])
    
    # 应用主题
    fig.update_layout(
        plot_bgcolor=colors["background"],
        paper_bgcolor=colors["background"],
        font=dict(
            color=colors["text"]
        ),
        xaxis=dict(
            gridcolor=colors["grid"],
            zerolinecolor=colors["grid"]
        ),
        yaxis=dict(
            gridcolor=colors["grid"],
            zerolinecolor=colors["grid"]
        )
    )
    
    # 更新K线图颜色
    for trace in fig.data:
        if trace.type == "candlestick":
            trace.increasing.line.color = colors["up"]
            trace.increasing.fillcolor = colors["up"]
            trace.decreasing.line.color = colors["down"]
            trace.decreasing.fillcolor = colors["down"]
    
    return fig

def set_default_theme(theme: str = "default") -> None:
    """
    设置默认主题
    
    参数:
        theme: 主题名称，可选值: "default", "dark", "light", "colorblind"
    """
    # 获取主题颜色
    colors = THEME_COLORS.get(theme, THEME_COLORS["default"])
    
    # 创建自定义主题
    custom_theme = {
        "layout": {
            "plot_bgcolor": colors["background"],
            "paper_bgcolor": colors["background"],
            "font": {"color": colors["text"]},
            "xaxis": {
                "gridcolor": colors["grid"],
                "zerolinecolor": colors["grid"]
            },
            "yaxis": {
                "gridcolor": colors["grid"],
                "zerolinecolor": colors["grid"]
            }
        },
        "colorway": [
            colors["primary"],
            colors["secondary"],
            colors["tertiary"],
            colors["quaternary"],
            colors["quinary"]
        ]
    }
    
    # 设置默认主题
    pio.templates["custom"] = custom_theme
    pio.templates.default = "custom"

def create_custom_theme(
    name: str,
    colors: Dict[str, str],
    base_theme: str = "default"
) -> Dict[str, Any]:
    """
    创建自定义主题
    
    参数:
        name: 主题名称
        colors: 主题颜色字典
        base_theme: 基础主题名称
    
    返回:
        主题颜色字典
    """
    # 获取基础主题颜色
    base_colors = THEME_COLORS.get(base_theme, THEME_COLORS["default"]).copy()
    
    # 更新颜色
    base_colors.update(colors)
    
    # 添加到主题颜色字典
    THEME_COLORS[name] = base_colors
    
    return base_colors